package com.example.zg.learn;

import android.support.annotation.DrawableRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Scroller;

import com.example.zg.learn.fragment.BookcaseFragment;
import com.example.zg.learn.fragment.BookstoreFragment;
import com.example.zg.learn.fragment.FindFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * fragment 布局切换横竖屏重叠
 */
public class FragmentActivity extends AppCompatActivity {
    private static final String TAG = "FragmentActivity>>>";
    private TabLayout mTabLayout;
    private List<TabModel> tabModels;
    private List<Fragment> fragments;
    private int page=0;//当前显示的哪页

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        if(savedInstanceState!=null){
            Log.i(TAG, "onCreate: 重建");
            page=savedInstanceState.getInt("page");
            findFragments();
        }else{
            Log.i(TAG, "onCreate: 新建");
            initFragments();
        }
        
        initTabLayout();
        //tabLayout默认显示的第0个,当page=0是要设置显示的Fragment
        if(page==0){
            showFragment(page);
        }else {
            //page不为0时,让tabLayout选中第page个,触发监听设置Fragment
            mTabLayout.getTabAt(page).select();
        }

    }
    
    private void initFragments(){
        Log.i(TAG, "initFragments: ");
        fragments=new ArrayList<>();
        fragments.add(new BookcaseFragment());
        fragments.add(new BookstoreFragment());
        fragments.add(new FindFragment());
    }
    
    private void findFragments(){
        Log.i(TAG, "findFragments: ");
        fragments=new ArrayList<>();
        FragmentManager fragmentManager=getSupportFragmentManager();
        
        Fragment fragment1 = fragmentManager.findFragmentByTag(BookcaseFragment.class.getSimpleName());
        if(fragment1==null){
            fragment1=new BookcaseFragment();
        }else{
            Log.i(TAG, "findFragments: 找到了BookcaseFragment");
        }
        fragments.add(fragment1);

        fragment1 = fragmentManager.findFragmentByTag(BookstoreFragment.class.getSimpleName());
        if(fragment1==null){
            fragment1=new BookstoreFragment();
        }else{
            Log.i(TAG, "findFragments: 找到了BookstoreFragment");

        }
        fragments.add(fragment1);

        fragment1 = fragmentManager.findFragmentByTag(FindFragment.class.getSimpleName());
        if(fragment1==null){
            fragment1=new FindFragment();
        }else{
            Log.i(TAG, "findFragments: 找到了FindFragment");

        }
        fragments.add(fragment1);

    }
    
    private void showFragment(int index){
        Fragment fragment = fragments.get(index);
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if(fragment.isAdded()){
            if(fragment.isHidden()){
                Log.i(TAG, "showFragment: 显示fragment:"+index);
                ft.show(fragment);
            }


        }else{
            Log.i(TAG, "showFragment: 添加fragment:"+index);
            ft.add(R.id.frameLayout,fragment,fragment.getClass().getSimpleName());
        }
        ft.commit();
        page=index;

    }

    private void hideFragment( int index){
        Fragment fragment = fragments.get(index);
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if(fragment.isAdded() && !fragment.isHidden()){
            Log.i(TAG, "showFragment: 隐藏fragment:"+index);
            ft.hide(fragment);
        }
        ft.commit();
    }

    
    
    private void initTabLayout(){
        mTabLayout=findViewById(R.id.tabLayout);
        tabModels=new ArrayList<>();
        tabModels.add(new TabModel("书架",R.drawable.bookcase));
        tabModels.add(new TabModel("精品",R.drawable.bookstore));
        tabModels.add(new TabModel("发现",R.drawable.find));
        for(TabModel model:tabModels){
            mTabLayout.addTab(mTabLayout.newTab().setText(model.name).setIcon(model.iconId));
        }
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.i(TAG, "onTabSelected:"+tab.getPosition()+" 未选中->选中");
                showFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                Log.i(TAG, "onTabUnselected:"+tab.getPosition()+" 选中->未选中");
                hideFragment(tab.getPosition());

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Log.i(TAG, "onTabReselected:"+tab.getPosition()+" 选中->选中");

            }
        });

    }

    private class TabModel{
        private String name;
        private @DrawableRes int iconId;

        private TabModel(String name, int iconId) {
            this.name = name;
            this.iconId = iconId;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("page",page);
    }
}
